package com.example.basic_config.config;

import com.example.basic_config.interceptor.MyInterceptor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.*;
import org.springframework.web.servlet.view.InternalResourceViewResolver;
import org.springframework.web.servlet.view.JstlView;

/**
 * @author ljw
 * @description
 * @date 2021/8/31 8:12
 */
@Configuration
@EnableWebMvc
@ComponentScan("com.example.basic_config")
public class MyMvcConfig extends WebMvcConfigurerAdapter {

    /**
     * 内部资源视图解析器
     * @author ljw
     * @date 2021/9/1 8:14
     */
    @Bean
    public InternalResourceViewResolver viewResolver() {
        InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
        viewResolver.setPrefix("/WEB-INF/classes/assets/views/");
        viewResolver.setSuffix(".jsp");
        viewResolver.setViewClass(JstlView.class);
        return viewResolver;
    }

    /**
     * 静态资源映射
     * @param registry
     * @author ljw
     * @date 2021/9/2 7:10
     */
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/assets/**").addResourceLocations("classpath:/assets/");
    }

    /**
     * 自定义拦截器
     * @param
     * @author ljw
     * @date 2021/9/2 7:24
     */
    @Bean
    public MyInterceptor myInterceptor() {
        return new MyInterceptor();
    }

    /**
     * 注册自定义拦截器
     * @param
     * @author ljw
     * @date 2021/9/2 7:25
     */
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(myInterceptor());
    }

    /**
     * 注册页面跳转
     * @param
     * @author ljw
     * @date 2021/9/7 7:19
     */
    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        registry.addViewController("/myPage.hello").setViewName("/hello");
    }

    /**
     * 路径匹配规则
     * @param
     * @author ljw
     * @date 2021/9/7 7:21
     */
    @Override
    public void configurePathMatch(PathMatchConfigurer configurer) {
        configurer.setUseSuffixPatternMatch(false);
    }
}
